Add Liquidity
To add liquidity to a pool, a liquidity transaction needs to be generated by calling the Liquidity Add
endpoint in the Rest API and then broadcast to a chain.
The Add Liquidity parameters are the following:
pair_info
token_1_liquidity
token_2_liquidity
slippage_tolerance
timeout
We will go through all the steps needed to get each of the parameters.
Steps
The following steps describe the workflow to add liquidity to a pool using the Euclid Layer:
1. Get all available pools
The first step of the process will be retrieving all the available VLP. This can be done using the All VLPS query:
You can use pagination to iterate through all the pools as you like.
query All_vlps {
router {
all_vlps {
vlps {
vlp
token_1
token_2
}
}
}
}
This will return all the available pools in the Euclid Layer:
{
"data": {
"router": {
"all_vlps": {
"vlps": [
{
"vlp": "nibi1gd52zq92wexpv0jwzktkma5krxzgkuupqlltx6qxqv4u8xp39d3qa5jdxk",
"token_1": "osmo",
"token_2": "sol"
},
{
"vlp": "nibi1x34fkar6puj4v3n0h84xf5y03u3uw5z5ggk95asql2cey6u8pygqlfups2",
"token_1": "osmo",
"token_2": "usdt"
},
{
"vlp": "nibi15gt9jedsst7ggvnm70x4jvplzy050npgmtlej87zdlys8tj2w0jsypk42k",
"token_1": "pepe",
"token_2": "usdt"
},
{
"vlp": "nibi1aqxyhput7y6g5hq8mssrgs86yzkfe7wkrruz6ymhh2589rg5am8sw927mk",
"token_1": "usdc",
"token_2": "usdt"
}
]
}
}
}
}
Using this data, you can prompt the user to select the pool they wish to provide liquidity to. You will also need the address of the VLP, so make sure to keep that saved.
2. Select the chain to add liquidity on
Now that we have chosen the token pair to add liquidity to, we can check the chains that can be used to add liquidity. This can be done using the All Pools query:
Use the contract address of the VLP contract for the token pair selected by the user.
query Vlp($contract: String!) {
vlp(contract: $contract) {
all_pools {
pools {
chain_uid
pool {
reserve_1
reserve_2
lp_shares
}
}
}
}
}
The response will retrieve the chains where this pair exists (usdc-usdt
in this example):
{
"data": {
"vlp": {
"all_pools": {
"pools": [
{
"chain_uid": "ethereum",
"pool": {
"reserve_1": "9998505649856",
"reserve_2": "10001756242906",
"lp_shares": "10000129999349"
}
},
{
"chain_uid": "osmosis",
"pool": {
"reserve_1": "9998375670523",
"reserve_2": "10001626221316",
"lp_shares": "9999999998900"
}
}
]
}
}
}
}
The user can select the pool they wish to add to.
3. Get the allowed denoms for each of the tokens
For each of the tokens in the pair, we need to get the allowed denoms. This will be used to specify the pair_info
field for the add liquidity call. The allowed denoms can be fetched using the Escrow query:
Use the chain_uid
from the last step and token Id
from step 1.
query Escrow($chainUid: String!, $tokenId: String) {
factory(chain_uid: $chainUid) {
escrow(token_id: $tokenId) {
escrow_address
denoms {
... on SmartTokenType {
smart {
contract_address
}
}
... on NativeTokenType {
native {
denom
}
}
}
}
}
}
The response will return the denom to be used:
{
"escrow": {
"escrow_address": "wasm1e7tjyf2ewepspakgjhfynzcxtw22ah0sltzkflpyep3dvldju8gqz0750q",
"denoms": [
{
"native": {
"denom": "uusdca"
}
}
]
}
}
We then perform the same query for the second token in the pool getting the denom for it as well.
4. Specify the amount of liquidity to be added
The user can specify the amount of liquidity to add for one of the tokens, and we can calculate the seconds amount automatically. Given the amount of token 1 to add, the second amount can be calculated as follows:
This ensures that the ratio of liquidity remains the same.
5. Generate Add Liquidity transaction
- Use the responses we got in all the previous steps for the fields.
- For sender address and chain_uid use the ones from the connected chain. In the example below, we are using a Keplr wallet.
- You can add (prompt the user) the slippage tolerance for the transaction. Specified as a pecent between 1 and 100.
We now have everything needed to generate the Add Liquidity transaction message:
const msg = await REST_AXIOS.post("/execute/liquidity/add", {
token_1_liquidity: token1Amount, // liquidity to add for token 1
token_2_liquidity: token2Amount, // liquidity to add for token 1
slippage_tolerance: slippageTolerance, // amount of slippage tolerated in %
pair_info: pair, // information regarding the token pair to add liquidity to
sender: {
address: wallet!.bech32Address,
chain_uid: chain!.chain_uid,
},
}).then((res) => res.data as TxResult);
console.log(msg.msgs, "Liquidity Response");
6. Broadcast the transaction to chain
The final step will be broadcasting this transaction to the chain and signing it with the connected wallet:
const tx = await client!.executeMultiple(
wallet!.bech32Address,
msg.msgs,
"auto",
"Add Liquidity"
);
return tx;